package com.noahsloan.nutils.collections;

import com.noahsloan.nutils.Equals;
import com.noahsloan.nutils.Hasher;

/**
 * Base class for classes needing to wrap values to replace their hashCode and
 * equals methods.
 * 
 * @author noah
 * 
 * @param <T>
 */
public abstract class ExternalHashBase<T> {

	protected class HashWrapper {
		final T value;

		public HashWrapper(T value) {
			super();
			this.value = value;
		}

		@Override
		public int hashCode() {
			return hash.hashCode(value);
		}

		/**
		 * Uses the external equals. If obj is not of the same HashWrapper
		 * class, returns false.
		 */
		@Override
		public boolean equals(Object obj) {
			if (obj == null || obj.getClass() != HashWrapper.class) {
				return false;
			}
			return equals.equals(value, ((HashWrapper) obj).value);
		}
	}

	protected final Hasher<? super T> hash;

	protected final Equals<? super T, ? super T> equals;

	protected ExternalHashBase(Hasher<? super T> hash, Equals<? super T, ? super T> equals) {
		super();
		this.hash = hash;
		this.equals = equals;
	}

}
